package com.zm.controller;

import com.zm.bean.CodeEnum;
import com.zm.bean.ResultBean;
import com.zm.bean.UserBean;
import com.zm.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping(value = "/user")
public class UserController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Resource
    UserService userService;
//
//    @Autowired
//    UserMapper userMapper;

//    @ApiOperation(value = "添加用户信息", notes = "添加用户信息接口")
//    @RequestMapping(value = "/save.do", method = RequestMethod.GET)
//    @ResponseBody
//    public ResultBean save(UserBean2 userBean) {
//        userBean.setUserName("test1");
//        userBean.setPassword("pwd1");
//        userBean.setAge(10);
//        userService.save(userBean);
//
//        return new ResultBean(CodeEnum.SUCCESS, null);
//    }

//    @ApiOperation(value = "获取用户列表", notes = "获取用户列表接口")
//    @RequestMapping(value = "/getUserList.do", method = RequestMethod.GET)
//    @ResponseBody
//    public ResultBean getUserList() {
//
//        List<UserBean2> userBeans = userService.getUserList();
//
//        return new ResultBean(CodeEnum.SUCCESS, userBeans);
//    }


//    @Test
//    public void testFindByCondition()  {
//        int page=0,size=10;
//        Sort sort = new Sort(Sort.Direction.DESC, "id");
//        Pageable pageable = PageRequest.of(page, size, sort);
//        UserDetailParam param=new UserDetailParam();
//        param.setIntroduction("程序员");
//        param.setMinAge(10);
//        param.setMaxAge(30);
//        Page<UserDetail> page1=userDetailService.findByCondition(param,pageable);
//        for (UserDetail userDetail:page1){
//            System.out.println("userDetail: "+userDetail.toString());
//        }
//    }


    @RequestMapping("/getUsers")
    @ResponseBody
    public ResultBean getUsers() {
        List<UserBean> users = userService.getAllUsers();
//        List<UserBean2> users = userMapper.getAllUsers();
        System.out.print(" users.size()=" + users.size());


        return new ResultBean(CodeEnum.SUCCESS, users);
    }

//    @RequestMapping("/getUser")
//    public UserBean2 getUser(Long id) {
//        UserBean2 user = userMapper.getOne(id);
//        return user;
//    }
//
//    @RequestMapping("/add")
//    public void save(UserBean2 user) {
//        userMapper.insert(user);
//    }
//
//    @RequestMapping(value = "update")
//    public void update(UserBean2 user) {
//        userMapper.update(user);
//    }
//
//    @RequestMapping(value = "/delete/{id}")
//    public void delete(@PathVariable("id") Long id) {
//        userMapper.delete(id);
//    }


    @RequestMapping("/getUser")
    @Cacheable(value = "user-key")
    @ResponseBody
    public UserBean getUser() {
        UserBean user = new UserBean();
//        user.setId(1);
//        user.setUserName("zcx");
//        user.setPassword("zcxpwd");
//        user.setAge(28);
//        System.out.println("若下面没出现“无缓存的时候调用”字样且能打印出数据表示测试成功");
        return user;
    }


//    @RequestMapping("/uid")
//    @ResponseBody
//    String uid(HttpSession session) {
//        UUID uid = (UUID) session.getAttribute("uid");
//        if (uid == null) {
//            uid = UUID.randomUUID();
//        }
//        session.setAttribute("uid", uid);
//        return session.getId();
//    }

}

